package com.hidt.shop.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.hidt.shop.domain.Goods;
import com.hidt.shop.domain.Goods_category;
import com.hidt.shop.domain.Goods_class;
import com.hidt.shop.domain.S_goods;
import com.hidt.shop.service.GoodsClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@ResponseBody
public class Goods_ClassController {

    @Autowired
    private GoodsClassService goodsService;

/**
 *@methodName select_class
 *@方法功能描述
 *  页面发送请求
 *  查询大小类
 *@params
 *@return java.util.List<com.hidt.shop.domain.Goods_class>
 *@exception
 *@author $風~$
 *@Date 2021/1/18
 */
    @RequestMapping("/selectClass")
    @ResponseBody
    public List<Goods_class> select_class(){
        System.out.println("查找大小类~~~~~~~~~~~~~~~~~~");
        List<Goods_class> goods_class = goodsService.find_goods_class();
        return  goods_class;
    }

    /**
     *@methodName find_Goods_BigClass
     *@方法功能描述
     * 查询大类
     *@params
     *@return java.util.List<com.hidt.shop.domain.Goods_class>
     *@exception
     *@author $風~$
     *@Date 2021/1/23
     */
    @RequestMapping("/selectBigClass")
    @ResponseBody
    public List<Goods_class> find_Goods_BigClass(){
        System.out.println("查找大类~~~~~~~~~~~~~~~~~~");
        List<Goods_class> goods_class = goodsService.find_goods_bigclass();
        return  goods_class;
    }


/**
 *@methodName select_little_class
 *@方法功能描述
 * 通过大类查询小类
 *@params
 * @param map
 *@return java.util.List
 *@exception
 *@author $風~$
 *@Date 2021/1/18
 */
    @RequestMapping("/selectLittleClass")
    public List select_little_class(@RequestBody  Map<String,String>  map) throws JsonProcessingException {
        System.out.println("查找小分类`````````````````````");
        List<Goods_category> goods_categories = goodsService.find_goods_categoryByClass(Integer.parseInt((String) map.get("class_id")));
        for(Object aaa:goods_categories){
            System.out.println(aaa);
        };
        return  goods_categories;
    }


    @RequestMapping("/selectAllClass")
    public  List selectAllClass() {
        System.out.println("查询全部");
//        List <Object> objects = new ArrayList<>();

        //查找全部大类
        List<Goods_class> goods_class = goodsService.find_goods_class();
//        Map<String,String> map = new HashMap<>();

//        通过大类查找全部小类
//        for (Goods_class big:goods_class) {
            //通过大类id 查找所有的小类信息
//            List<Goods_category> goods_categoryByClass = goodsService.find_goods_categoryByClass(big.getClass_id());
//            for (Goods_category lis:goods_categoryByClass){
//
//                String goods_category = " category_id:"+lis.getCategory_id() + " category_name:"+lis.getCategory_name();
//                map.put("goods_category",goods_category);

//            }

//        }
//        [{大类名字，[小类集合]}，{大类名字，[小类集合]}  ]
        return  goods_class;
    }



/**
 *@methodName add_Goods_Class
 *@方法功能描述
 * 添加大类
 *@params
 * @param map
 *@return int
 *@exception
 *@author $風~$
 *@Date 2021/1/19
 */
    @RequestMapping("/addGoodsClass")
    public  int add_Goods_Class (@RequestBody Map<String,String> map) throws  JsonProcessingException{
        System.out.println("添加大类~~~~~~~~~~~~~~~~~~~~~");

        int i = goodsService.add_Goods_Class(map);

        return  i;
    }



    /**
     *@methodName add_Goods_Category
     *@方法功能描述
     * 添加小类
     *@params
     * @param map
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/19
     */
    @RequestMapping("/addGoodsCategory")
    public int add_Goods_Category(@RequestBody Map<String,String> map) throws  JsonProcessingException{
        System.out.println("添加小类~~~~~~~~~~~~~~~~~~~~~");

        int i = goodsService.add_Goods_Category(map);

        return i;
    }



    /**
     *@methodName delete_Goods_Class
     *@方法功能描述
     * 刪除大类
     * 实际为修改classis_delete 的值
     * 数据库中
     * 1为删除 0为未删
     *@params
     * @param map
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/19
     */
    @RequestMapping("/deleteGoodsClass")
    public int delete_Goods_Class(@RequestBody Map<String,String> map) throws JsonProcessingException{
        int i = goodsService.delete_Goods_Class(map);

        return i;
    }


    /**
     *@methodName delete_Goods_Category
     *@方法功能描述
     * 删除小类
     * 实际为修改
     * categoryis_delete的值为1
     * 数据库中
     * 1为删除  0为未删
     *@params
     * @param map
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/19
     */
    @RequestMapping("/deleteGoodsCategory")
    public int delete_Goods_Category(@RequestBody Map<String,String> map) throws JsonProcessingException{
        int i = goodsService.delete_Goods_Category(map);

        return i;
    }



    /**
     *@methodName update_Goods_Class
     *@方法功能描述
     * 修改大类名
     * 需要 大类的id   新类名
     *@params
     * @param map
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/19
     */
    @RequestMapping("/updateGoodsClass")
    public int update_Goods_Class(@RequestBody Map<String,String> map) throws  JsonProcessingException{
        int i = goodsService.update_Goods_Class(map);
        return i;
    }

    /**
     *@methodName update_Goods_Category
     *@方法功能描述
     * 修改小类
     * 需要小类的id  旧的name 所属分类  新的name 所属分类
     *@params
     * @param map
     *@return int
     *@exception
     *@author $風~$
     *@Date 2021/1/19
     */
    @RequestMapping("/updateGoodsCategory")
    public  int update_Goods_Category(@RequestBody Map<String,String> map) throws  JsonProcessingException{
        int i = goodsService.update_Goods_Category(map);
        return i;
    }



/**
 *@methodName selete_Goods_ByClassId
 *@方法功能描述
 * 页面传过来大类的id 返回商品全部信息
 *@params
 * @param map
 *@return java.util.List<com.hidt.shop.domain.Goods>
 *@exception
 *@author $風~$
 *@Date 2021/1/20
 */
    @RequestMapping("/selectGoodsByClassId")
    public List<Goods> selete_Goods_ByClassId(@RequestBody Map<String,String> map) throws  JsonProcessingException{
        List<Goods> goodsList = goodsService.selete_Goods_ByClassId(Integer.parseInt(map.get("class_id")));
        return goodsList;
    }



/**
 *@methodName selete_Goods_ByCategoryId
 *@方法功能描述
 * 页面传过来小类的id 返回商品全部信息
 *@params
 * @param map
 *@return java.util.List<com.hidt.shop.domain.Goods>
 *@exception
 *@author $風~$
 *@Date 2021/1/20
 */
    @RequestMapping("/selectGoodsByCategoryId")
    public List<Goods> selete_Goods_ByCategoryId(@RequestBody Map<String,String> map) throws  JsonProcessingException{
        List<Goods> goodsList = goodsService.selete_Goods_ByCategoryId(Integer.parseInt(map.get("category_id")));
        return goodsList;
    }


/***
 *@methodName select_goods_class_list
 *@方法功能描述
 *@params
 *@return java.util.List<com.hidt.shop.domain.Goods_class>
 *@exception
 *@author $風~$
 *@Date 2021/2/23
 */
    @RequestMapping("/selectGoodsClassList")
    public List<Goods_class> select_goods_class_list(){
        List<Goods_class> goodsList = goodsService.select_goods_class_list();
        return goodsList;
    }

/***
 *@methodName fselect_goods_class_list
 *@方法功能描述
 * 首页的d
 *@params
 *@return java.util.List<com.hidt.shop.domain.Goods_class>
 *@exception
 *@author $風~$
 *@Date 2021/2/23
 */
    @RequestMapping("/fselectGoodsClassList")
    public List<Goods_class> fselect_goods_class_list() {
        List<Goods_class> goodsList = goodsService.select_goods_class_list();
        ArrayList<Goods_class> list = new ArrayList<Goods_class>();
        for (int i = 0;i<goodsList.size();i++){
            System.out.println(goodsList.get(i).getClass_id());
            if (goodsList.get(i).getClass_id() == 1 ){
                goodsList.get(i).setGoods_list(goodsList.get(i).getGoods_list().subList(0,7));
                list.add(goodsList.get(i));
            }
            if (goodsList.get(i).getClass_id() == 8 ){
                goodsList.get(i).setGoods_list(goodsList.get(i).getGoods_list().subList(0,7));
                list.add(goodsList.get(i));

            }
            if (goodsList.get(i).getClass_id() == 10){
                goodsList.get(i).setGoods_list(goodsList.get(i).getGoods_list().subList(0,7));
                list.add(goodsList.get(i));
            }

        }
        return list;
    }


}

